<?php
App::uses('AppController', 'Controller');
/**
 * Products Controller
 *
 * @property Product $Product
 */
class ProductsController extends AppController {

/**
 * index method
 *
 * @return void
 */
	public function index() {
		$this->Product->recursive = 0;
		$products = $this->Product->find('all', array('conditions' => array('Product.archive' => '0')));
		$this->set(compact('products'));
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		if (!$this->Product->exists($id)) {
			throw new NotFoundException(__('Invalid product'));
		}
		$options = array('conditions' => array('Product.' . $this->Product->primaryKey => $id));
		$this->set('product', $this->Product->find('first', $options));
        
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
		if ($this->request->is('post')) {
			$this->Product->create();
			$data = $this->request->data;
			if(empty($data['Product']['suppliers_id'])){
				$supplier = $this->Product->Suppliers->query('SELECT id FROM Suppliers WHERE supplierName = "No Supplier"');
				$nosupid = $supplier['0']['Suppliers']['id'];
				$data = Set::insert($data, 'Product.suppliers_id', $nosupid);
			}
			if ($this->Product->save($data)) {
				$this->Session->setFlash(__('The product has been saved'),'default', array('class' => 'savesuccess'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The product could not be saved. Please, try again.'),'default', array('class' => 'saveerror'));
			}
        } 
            
        $productsbrands = $this->Product->Productsbrand->find('list',array('order'=>'name ASC', 'conditions'=>array('archive'=>'0')));
        $this->set(compact('productsbrands'));
        $productsranges = $this->Product->Productsrange->find('list',array('order'=>'name ASC', 'conditions'=>array('archive'=>'0')));
        $this->set(compact('productsranges'));
        $productscategories = $this->Product->Productscategory->find('list',array('order'=>'name ASC', 'conditions'=>array('archive'=>'0')));
        $this->set(compact('productscategories'));
        $supplier = $this->Product->Suppliers->find('all', array('order'=>'supplierName ASC','conditions' => array('archive' => '0')));
		$suppliers = array();
		foreach($supplier as $i=>$j){
			$suppliers = Set::insert($suppliers, $j['Suppliers']['id'], $j['Suppliers']['supplierName']);
		}
		$this->Set(compact('suppliers'));                   
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		if (!$this->Product->exists($id)) {
			throw new NotFoundException(__('Invalid product'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Product->save($this->request->data)) {
				$this->Session->setFlash(__('The product has been saved'),'default', array('class' => 'savesuccess'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The product could not be saved. Please, try again.'),'default', array('class' => 'saveerror'));
			}
		} else {
			$options = array('conditions' => array('Product.' . $this->Product->primaryKey => $id));
			$this->request->data = $this->Product->find('first', $options);
		}
		
        $productsbrands = $this->Product->Productsbrand->find('list',array('order'=>'name ASC'));
        $this->set(compact('productsbrands'));
        $productsranges = $this->Product->Productsrange->find('list',array('order'=>'name ASC'));
        $this->set(compact('productsranges'));
        $productscategories = $this->Product->Productscategory->find('list',array('order'=>'name ASC'));
        $this->set(compact('productscategories'));
        $supplier = $this->Product->Suppliers->find('all', array('order'=>'supplierName ASC','conditions' => array('archive' => '0')));
		$suppliers = array();
		foreach($supplier as $i=>$j){
			$suppliers = Set::insert($suppliers, $j['Suppliers']['id'], $j['Suppliers']['supplierName']);
		}
		$this->Set(compact('suppliers')); 
	}

/**
 * delete method
 *
 * @throws NotFoundException
 * @throws MethodNotAllowedException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		$this->Product->id = $id;
		if (!$this->Product->exists()) {
			throw new NotFoundException(__('Invalid product'));
		}
		$this->request->onlyAllow('post', 'delete');
		$data = array('id' => $id, 'archive' => '1');
		if ($this->Product->save($data)) {

			//archive productsintance
			$pi = $this->Product->Productsinstance->find('list', array('conditions'=>array('products_id'=>$id)));
			foreach ($pi as $j) {
				$data = array('id' => $j, 'archive' => '1');
				$this->Product->Productsinstance->save($data);
			}

			//archive ordersproduct
			$this->loadModel('Ordersproduct');
			$this->loadModel('Deliveriesordersproduct');
			$op = $this->Ordersproduct->find('list', array('conditions'=>array('orders_id'=>$id)));
			foreach ($op as $j) {

				//archive deliveriesordersproduct
				$dop = $this->Deliveriesordersproduct->find('list', array('conditions'=>array('ordersproducts_id'=>$j)));
				foreach ($dop as $k) {
					$data = array('id' => $k, 'archive' => '1');
					$this->Deliveriesordersproduct->save($data);
				}

				$data = array('id' => $j, 'archive' => '1');
				$this->Ordersproduct->save($data);
			}

			$this->Session->setFlash(__('Product deleted'),'default', array('class' => 'savesuccess'));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('Product could not be deleted'),'default', array('class' => 'saveerror'));
		$this->redirect(array('action' => 'index'));
	}

/**
 * unarchive method
 *
 * @throws NotFoundException
 * @throws MethodNotAllowedException
 * @param string $id
 * @return void
 */
	public function unarchive($id = null) {
		$this->Product->id = $id;
		if (!$this->Product->exists()) {
			throw new NotFoundException(__('Invalid product'));
		}
		$this->request->onlyAllow('post');
		$data = array('id' => $id, 'archive' => '0');
		if ($this->Product->save($data)) {

			//archive productsintance
			$pi = $this->Product->Productsinstance->find('list', array('conditions'=>array('products_id'=>$id)));
			foreach ($pi as $j) {
				$data = array('id' => $j, 'archive' => '0');
				$this->Product->Productsinstance->save($data);
			}

			//archive ordersproduct
			$this->loadModel('Ordersproduct');
			$this->loadModel('Deliveriesordersproduct');
			$op = $this->Ordersproduct->find('list', array('conditions'=>array('orders_id'=>$id)));
			foreach ($op as $j) {

				//archive deliveriesordersproduct
				$dop = $this->Deliveriesordersproduct->find('list', array('conditions'=>array('ordersproducts_id'=>$j)));
				foreach ($dop as $k) {
					$data = array('id' => $k, 'archive' => '0');
					$this->Deliveriesordersproduct->save($data);
				}

				$data = array('id' => $j, 'archive' => '0');
				$this->Ordersproduct->save($data);
			}

			$this->Session->setFlash(__('Product un-archived.'),'default', array('class' => 'savesuccess'));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('Product could not be un-archived.'),'default', array('class' => 'saveerror'));
		$this->redirect(array('action' => 'index'));
	}

/**
 * indexarchive method
 *
 * @return void
 */
	public function indexarchive() {
		$this->Product->recursive = 0;

		$products = $this->Product->find('all', array('conditions' => array('Product.archive' => '1')));
		$this->set(compact('products'));
	}

/**
 * indexnosupplier method
 *
 * @return void
 */
	public function indexnosupplier() {
		$this->Product->recursive = 0;

		$supplier = $this->Product->Suppliers->query('SELECT id FROM Suppliers WHERE supplierName = "No Supplier"');
		$nosupid = $supplier['0']['Suppliers']['id'];

		$products = $this->Product->find('all', array('conditions' => array('Product.archive' => '0', 'Product.suppliers_id' => $nosupid)));
		$this->set(compact('products'));
	}
}
